<?php
/*
 OS POS, Open Source Commerce Solutions

  Copyright (c) 2008 OS POS

  Released under the GNU General Public License
  Based on admin order by:
  Written by Jonathan Hilgeman of SiteCreative.com (osc@sitecreative.com)
  Version History

*/

  require('includes/application_top.php');
  require(DIR_WS_FUNCTIONS . 'c_orders.php');
  require(DIR_WS_CLASSES . 'currencies.php');
  $currencies = new currencies();

  // require(DIR_WS_CLASSES . 'PriceFormatter.php');
  // $pf = new PriceFormatter;

  include(DIR_WS_CLASSES . 'pos_order.php');

 // Optional Tax Rate/Percent
  $AddShippingTax = "0.0"; // e.g. shipping tax of 17.5% is "17.5"

  // intilize variables
  $update_info_cc_number = '';
  $update_info_cc_expires = '';
  $update_info_cc_ccv = '';
  $cc_ccv10 = '';
  $update_products = '';
  $update_totals = '';
  $add_products_model = '';
  $add_products_name = '';
  $add_products_price = '';
  $products_tax_class_id = '';
  $add_product_options = '';
  $item_has_down = '';
  $products_options_name = '';
  $AddedOptionsPrice = '';
  $customer_id = '' ;




        if (isset($_GET['product_count'])) {
              $product_count = $_GET['product_count'] ;
            }else if (isset($_POST['product_count'])){
              $product_count = $_POST['product_count'] ;
            } else {
             $product_count = '' ;
    }
  // New "Status History" table has different format.
  $OldNewStatusValues = (tep_field_exists(TABLE_POS_STATUS_HISTORY, "old_value") && tep_field_exists(TABLE_POS_STATUS_HISTORY, "new_value"));
  $CommentsWithStatus = tep_field_exists(TABLE_POS_STATUS_HISTORY, "comments");
  $SeparateBillingFields = tep_field_exists(TABLE_POS, "billing_name");



  // Optional Tax Rate/Percent
  $AddShippingTax = "0.0"; // e.g. shipping tax of 17.5% is "17.5"

  $pos_statuses = array();
  $pos_status_array = array();
  $pos_status_query = tep_db_query("select pos_status_id, pos_status_name from " . TABLE_POS_STATUS . " where language_id = '" . (int)$languages_id . "'");
  while ($pos_status = tep_db_fetch_array($pos_status_query)) {
    $pos_statuses[] = array('id' => $pos_status['pos_status_id'],
                               'text' => $pos_status['pos_status_name']);
    $pos_status_array[$pos_status['pos_status_id']] = $pos_status['pos_status_name'];
  }

//get variables

 if (isset($_GET['posID'])){
      $posID = $_GET['posID'];
    }else if (isset($_POST['posID'])){
      $posID = $_POST['posID'] ;
    } else {
     $posID = '' ;
    }

if (isset($_GET['step'])) {
      $step = $_GET['step'] ;
    }else if (isset($_POST['step'])){
      $step = $_POST['step'] ;
    } else {
     $step = 1 ;
    }

// begin action
if (isset($_GET['action'])) {
      $action = $_GET['action'] ;
    }else if (isset($_POST['action'])){
      $action = $_POST['action'] ;
    } else {
     $action = 'edit' ;
    }

  if (tep_not_null($action)) {
    switch ($action) {
case 'update_customer':
  //get customer info
  if (isset($_POST['update_customer_customers_id'])){
        $update_customer_customers_id = $_POST['update_customer_customers_id'] ;
    }

if (isset($update_customer_customers_id)) {
  //second get customer infor from adress book


      $account_query = tep_db_query("select * from " . TABLE_CUSTOMERS . " where customers_id = '" . $update_customer_customers_id . "'");
      $account = tep_db_fetch_array($account_query);

      $customer = $_GET['Customer'];
      $address_query = tep_db_query("select * from " . TABLE_ADDRESS_BOOK . " where customers_id = '" . $update_customer_customers_id . "'");
      $address = tep_db_fetch_array($address_query);

      $gender = (isset($_POST['gender']) ? tep_db_prepare_input($_POST['gender']) : '' );
      $firstname = $account['customers_firstname'];
      $lastname = $account['customers_lastname'];
      $dob = $account['customers_dob'];
      $email_address = $account['customers_email_address'];
      $telephone = (isset($account['customers_telephone']) ? $account['customers_telephone'] : '');
      $fax =  (isset($account['customers_fax']) ? $account['customers_fax'] : '');
      $newsletter = $account['customers_newsletter'];
      $password = $account['customers_password'];
      $confirmation_1 = $account['customers_password'];
      $street_address = $address['entry_street_address'];
      $company = $address['entry_company'];
      $suburb = $address['entry_suburb'];
      $postcode = $address['entry_postcode'];
      $city = $address['entry_city'];
      $zone_id = $address['entry_zone_id'];
      $state = $address['entry_state'];
      //convert from id to name
         $country_name = tep_get_country_name($address['entry_country_id']);
      $country = $country_name;
  }

  $error = false; // reset error flag
  //update pos table

     $sql_data_array = array('customers_id' => $customer_id,
              'customers_name' => $firstname . ' ' . $lastname,
              'customers_company' => $company,
              'customers_street_address' => $street_address,
              'customers_suburb' => $suburb,
              'customers_city' => $city,
              'customers_postcode' => $postcode,
              'customers_state' => $state,
              'customers_country' => $country,
              'customers_telephone' => $telephone,
              'customers_email_address' => $email_address,
              'customers_address_format_id' => $format_id,
              'delivery_name' => $firstname . ' ' . $lastname,
              'delivery_company' => $company,
              'delivery_street_address' => $street_address,
              'delivery_suburb' => $suburb,
              'delivery_city' => $city,
              'delivery_postcode' => $postcode,
              'delivery_state' => $state,
              'delivery_country' => $country,
              'delivery_address_format_id' => $format_id,
              'billing_name' => $firstname . ' ' . $lastname,
              'billing_company' => $company,
              'billing_street_address' => $street_address,
              'billing_suburb' => $suburb,
              'billing_city' => $city,
              'billing_postcode' => $postcode,
              'billing_state' => $state,
              'billing_country' => $country,
              'billing_address_format_id' => $format_id,
               );
  //old

  tep_db_perform(TABLE_POS, $sql_data_array, 'update', 'pos_id=' . $posID);

 tep_redirect(tep_href_link(FILENAME_CREATE_POS_ORDERS, 'posID=' . $posID . '&action=edit', 'SSL'));
   break;
//******************************
  //start a new pos
  case 'start_order':
//pos_create_order_process.php
 // unset($action);
   tep_redirect(tep_href_link(FILENAME_POS_START_ORDER_PROCESS, 'posID=' . $posID, 'SSL'));

    break;
//**************************************
// BOF Admin Only Comments Box v1.2
        case 'update_admin_comments':
                $posID = tep_db_prepare_input($posID);
                $comments = tep_db_prepare_input($_POST['admin_comments']);
                $pos_updated = false;

                tep_db_query("insert into admin_comments (pos_id, date_added, comments) values ('" . (int)$posID . "', now(), '" . tep_db_input($comments)  . "')");

          $pos_updated = true;

        if ($pos_updated == true) {
         $messageStack->add_session('Admin Comments Updated', 'success');
        } else {
          $messageStack->add_session('Admin Comments Not Updated', 'warning');
        }

        tep_redirect(tep_href_link(FILENAME_CREATE_POS_ORDERS, 'posID=' . $posID . '&action=edit', 'SSL'));
        break;

//******************************


//******************************

 // Update Order
  case 'update_order':
    $pos_order = new pos($posID);
    $status = tep_db_prepare_input($_POST['status']);
    $comments = tep_db_prepare_input($_POST['comments']);
/*
    // Update Order Info
   $UpdateOrders = "update " . TABLE_POS . " set
      customers_name = '" . tep_db_input(stripslashes($_POST['update_customer_name'])) . "',
      customers_company = '" . tep_db_input(stripslashes($_POST['update_customer_company'])) . "',
      customers_street_address = '" . tep_db_input(stripslashes($_POST['update_customer_street_address'])) . "',
      customers_suburb = '" . tep_db_input(stripslashes($_POST['update_customer_suburb'])) . "',
      customers_city = '" . tep_db_input(stripslashes($_POST['update_customer_city'])) . "',
      customers_state = '" . tep_db_input(stripslashes($_POST['update_customer_state'])) . "',
      customers_postcode = '" . tep_db_input($_POST['update_customer_postcode']) . "',
      customers_country = '" . tep_db_input(stripslashes($_POST['update_customer_country'])) . "',
      customers_telephone = '" . tep_db_input($_POST['update_customer_telephone']) . "',
      customers_email_address = '" . tep_db_input($_POST['update_customer_email_address']) . "',";

    if($SeparateBillingFields)
    {
    $UpdateOrders .= "billing_name = '" . tep_db_input(stripslashes($_POST['update_billing_name'])) . "',
      billing_company = '" . tep_db_input(stripslashes($_POST['update_billing_company'])) . "',
      billing_street_address = '" . tep_db_input(stripslashes($_POST['update_billing_street_address'])) . "',
      billing_suburb = '" . tep_db_input(stripslashes($_POST['update_billing_suburb'])) . "',
      billing_city = '" . tep_db_input(stripslashes($_POST['update_billing_city'])) . "',
      billing_state = '" . tep_db_input(stripslashes($_POST['update_billing_state'])) . "',
      billing_postcode = '" . tep_db_input($_POST['update_billing_postcode']) . "',
      billing_country = '" . tep_db_input(stripslashes($_POST['update_billing_country'])) . "',";
    }

// Becasue of the way the form is written these variable may not be passed from the previous form
// so they must be checked to see if they are set if not the must be emtied,

//initlize variable
    $account_name = '';
    $account_number = '';
    $po_number = '';
    $cc_type = '';
    $cc_owner = '';
    $cc_start = '';
    $cc_issue = '';

//check to see if form passed these variable
    if(isset($_POST['account_name'])){
      $account_name = tep_db_input($_POST['account_name']);
      }
    if(isset($_POST['account_name'])){
      $account_number = tep_db_input($_POST['account_number']);
      }
    if(isset($_POST['po_number'])){
      $po_number = tep_db_input($_POST['po_number']);
      }
    if(isset($_POST['update_info_cc_type'])){
      $cc_type = tep_db_input($_POST['update_info_cc_type']);
      }
    if(isset($_POST['update_info_cc_owner'])){
      $cc_owner = tep_db_input($_POST['update_info_cc_owner']);
      }
    if(isset($_POST['update_info_cc_start'])){
      $cc_start = tep_db_input($_POST['update_info_cc_start']);
      }
    if(isset($_POST['update_info_cc_issue'])){
      $cc_issue = tep_db_input($_POST['update_info_cc_issue']);
      }

    $UpdateOrders .= "delivery_name = '" . tep_db_input(stripslashes($_POST['update_delivery_name'])) . "',
      delivery_company = '" . tep_db_input(stripslashes($_POST['update_delivery_company'])) . "',
      delivery_street_address = '" . tep_db_input(stripslashes($_POST['update_delivery_street_address'])) . "',
      delivery_suburb = '" . tep_db_input(stripslashes($_POST['update_delivery_suburb'])) . "',
      delivery_city = '" . tep_db_input(stripslashes($_POST['update_delivery_city'])) . "',
      delivery_state = '" . tep_db_input(stripslashes($_POST['update_delivery_state'])) . "',
      delivery_postcode = '" . tep_db_input($_POST['update_delivery_postcode']) . "',
      delivery_country = '" . tep_db_input(stripslashes($_POST['update_delivery_country'])) . "',
      payment_method = '" . tep_db_input($_POST['update_info_payment_method']) . "',
      account_name = '" . $account_name . "',
      account_number = '" . $account_number . "',
      po_number = '" . $po_number . "',
      cc_type = '" . $cc_type . "',
      cc_owner = '" . $cc_owner . "',
      cc_start = '" . $cc_start . "',
      cc_issue = '" . $cc_issue . "',
      last_modified = now(),";


// encrypt cc number begin
     $update_info_cc_number = (isset($_POST['update_info_cc_number']) ? $_POST['update_info_cc_number']  :'');
     $update_info_cc_expires = (isset($_POST['update_info_cc_expires']) ? str_replace('/', '', $_POST['update_info_cc_expires'])  :'');
     $update_info_cc_ccv = (isset($_POST['update_info_cc_ccv']) ? $_POST['update_info_cc_ccv']  :'');

         if ( (PAYMENT_CC_CRYPT == 'True') || (ereg ("([0-9]{4})", $pos_order->info['cc_number'])) ) {
          $cc_number5 = $pos_order->info['cc_number'];
             }else if ( (PAYMENT_CC_CRYPT == 'True') && !(ereg ("([0-9]{4})", $pos_order->info['cc_number'])) ){
          $cc_number5 = cc_decrypt($pos_order->info['cc_number']);
             }else{
          $cc_number5 =$pos_order->info['cc_number'];
             }

          if ($update_info_cc_number != $cc_number5){
            if ( (PAYMENT_CC_CRYPT == 'True') && (ereg ("([0-9]{4})", $update_info_cc_number)) ) {
                $cc_number10 = cc_encrypt($update_info_cc_number);
              }else if ( (PAYMENT_CC_CRYPT == 'True') && !(ereg ("([0-9]{4})", $update_info_cc_number)) ){
                 $cc_number10 = $update_info_cc_number;
              }else{
                 $cc_number10 =$update_info_cc_number;
              }

    $UpdateOrders .= " cc_number = '$cc_number10',";
                }
//encrypt cc number end  ;
// encrypt cc expire begin   ;
         if ( (PAYMENT_CC_CRYPT == 'True') || (ereg ("([0-9]{4})", $pos_order->info['cc_expires'])) ) {
          $cc_expires5 = $pos_order->info['cc_expires'];
          }else if ( (PAYMENT_CC_CRYPT == 'True') && !(ereg ("([0-9]{4})", $pos_order->info['cc_expires'])) ){
          $cc_expires5 = cc_decrypt($pos_order->info['cc_expires']);
          }else{
          $cc_expires5 =$pos_order->info['cc_expires'];
                  }

          if ($update_info_cc_expires != $cc_expires5){
            if ( (PAYMENT_CC_CRYPT == 'True') && (ereg ("([0-9]{4})", $update_info_cc_expires)) ) {
              $cc_expires10 = cc_encrypt($update_info_cc_expires);
            }else if ( (PAYMENT_CC_CRYPT == 'True') && !(ereg ("([0-9]{4})", $update_info_cc_expires)) ){
              $cc_expires10 = $update_info_cc_expires;
            }else{
              $cc_expires10 =$update_info_cc_expires;
            }

    $UpdateOrders .= " cc_expires = '$cc_expires10',";
                }
//encrypt cc expire end

//ccv  begin
         if ( (PAYMENT_CC_CRYPT == 'True') || (ereg ("([0-9]{3})", $pos_order->info['cc_ccv'])) ) {
           $cc_ccv5 = $pos_order->info['cc_ccv'];
         }else if ( (PAYMENT_CC_CRYPT == 'True') && !(ereg ("([0-9]{3})", $pos_order->info['cc_ccv'])) ){
           $cc_ccv5 = cc_decrypt($pos_order->info['cc_ccv']);
         }else{
           $cc_ccv5 =$pos_order->info['cc_ccv'];
         }

         if ($update_info_cc_ccv != $cc_ccv5){
           if ( (PAYMENT_CC_CRYPT == 'True') && (ereg ("([0-9]{3})", $update_info_cc_ccv)) ) {
             $cc_ccv10 = cc_encrypt($update_info_cc_ccv);
           }else if ( (PAYMENT_CC_CRYPT == 'True') && !(ereg ("([0-9]{3})", $update_info_cc_ccv)) ){
             $cc_ccv10 = $update_info_cc_ccv;
           }else{
             $cc_ccv10 = $update_info_cc_ccv;
               }
      }
    $UpdateOrders .= " cc_ccv = '$cc_ccv10' ";

//ccv end

    $UpdateOrders .= " where pos_id = '" . tep_db_input($posID) . "' ";

 //   tep_db_query($UpdateOrders);
*/
    $Query1 = "update pos set last_modified = now() where pos_id = '" . tep_db_input($posID) . "';";
    tep_db_query($Query1);
    $pos_updated = 1;

// status and comments

        $status = tep_db_prepare_input($_POST['status']);
        $comments = tep_db_prepare_input($_POST['comments']);


        $check_status_query = tep_db_query("select customers_name, customers_email_address, pos_status, date_purchased from " . TABLE_POS . " where pos_id = '" . (int)$posID . "'");
        $check_status = tep_db_fetch_array($check_status_query);

      // NOTE: you must post the order status to both the order, and order status history
      if($CommentsWithStatus)
// always update date and time on order_status
//check to see if can download status change
  if ( ($check_status['pos_status'] != $status) || $comments != '' || ($status ==DOWNLOADS_pos_status_UPDATED_VALUE) ) {
  tep_db_query("update " . TABLE_POS . " set pos_status = '" . tep_db_input($status) . "', last_modified = now() where pos_id = '" . (int)$posID . "'");
  $check_status_query2 = tep_db_query("select customers_name, customers_email_address, pos_status, date_purchased from " . TABLE_POS . " where pos_id = '" . (int)$posID . "'");
  $check_status2 = tep_db_fetch_array($check_status_query2);
  if ( $check_status2['pos_status']==DOWNLOADS_pos_status_UPDATED_VALUE ) {
    tep_db_query("update " . TABLE_POS_PRODUCTS_DOWNLOAD . " set download_maxdays = '" . tep_get_configuration_key_value('DOWNLOAD_MAX_DAYS') . "', download_count = '" . tep_get_configuration_key_value('DOWNLOAD_MAX_COUNT') . "' where pos_id = '" . (int)$posID . "'");
  }
  $customer_notified = '0';
  //if notify = 1 then send email update
  if (isset($_POST['notify']) && ($_POST['notify'] == 'on')) {
            $notify_comments = '';
/*
            if (isset($_POST['notify_comments']) && ($_POST['notify_comments'] == 'on')) {
              $notify_comments = sprintf(EMAIL_TEXT_COMMENTS_UPDATE, $comments) . "\n\n";
            }

             $email = STORE_NAME . "\n" . EMAIL_SEPARATOR . "\n" . EMAIL_TEXT_ORDER_NUMBER . ' ' . $posID . "\n" . EMAIL_TEXT_INVOICE_URL . ' ' .
        tep_catalog_href_link(FILENAME_CATALOG_ACCOUNT_HISTORY_INFO, 'order_id=' . $posID, 'SSL') . "\n" . EMAIL_TEXT_DATE_ORDERED .
        ' ' . tep_date_long($check_status['date_purchased']) . "\n\n" . $notify_comments .
        sprintf(EMAIL_TEXT_STATUS_UPDATE, $poss_status_array[$status]);

            tep_mail($check_status['customers_name'], $check_status['customers_email_address'], EMAIL_TEXT_SUBJECT, $email, STORE_OWNER, STORE_OWNER_EMAIL_ADDRESS);
*/
            $customer_notified = '1';
          }

          tep_db_query("insert into " . TABLE_POS_STATUS_HISTORY . " (pos_id, pos_status_id, date_added, customer_notified, comments) values ('" . (int)$posID . "', '" . tep_db_input($status) . "', now(), '" . tep_db_input($customer_notified) . "', '" . tep_db_input($comments)  . "')");

   $pos_updated = 1;

        }

    // check to see if there are products to update
    if (count($update_products) > 0)
    {
    // Update Products
    $RunningSubTotal = 0;
    $RunningTax = 0;
        // CWS EDIT (start) -- Check for existence of subtotals...
        // Do pre-check for subtotal field existence
    $ot_subtotal_found = 0;
//$total_details = $_GET['total_details'];
//update_totals

if (isset($_GET['update_totals'])) {
      $update_totals = $_GET['update_totals'] ;
    }else if (isset($_POST['update_totals'])){
      $update_totals = $_POST['update_totals'] ;
    } else {
     $update_totals = '' ;
    }
if (isset($_GET['update_products'])) {
      $update_products = $_GET['update_products'] ;
    }else if (isset($_POST['update_products'])){
      $update_products = $_POST['update_products'] ;
    } else {
     $update_products = '' ;
    }
     $ot_code = '';
      foreach($update_totals as $total_details)
    {
        extract($total_details,EXTR_PREFIX_ALL,"ot");

      if($ot_code == "ot_subtotal")
      {
          $ot_subtotal_found = 1;
          break;
      }
    }
    // CWS EDIT (end) -- Check for existence of subtotals...
    //check to see if any product as in order yet.
if (!(empty($update_products))) {
    foreach($update_products as $pos_products_id => $products_details)
    {

      // Update orders_products Table
      //UPDATE_INVENTORY_QUANTITY_START##############################################################################################################
      $pos_query = tep_db_query("select products_id, products_quantity from " . TABLE_POS_PRODUCTS . " where pos_products_id = '" . (int)$pos_products_id . "' and pos_id = '" .$posID . "' ");
     if (tep_db_num_rows($pos_query) > 0) {
        $pos_array = tep_db_fetch_array($pos_query);
      } else {
        $pos_array['products_quantity'] = 0;
      }
      if ($products_details['qty'] != $pos_array['products_quantity']) {
        $quantity_difference = (int)($products_details["qty"]) - (int)($pos_array['products_quantity']);
          $products_quantity = tep_db_fetch_array(tep_db_query("select products_quantity from " . TABLE_PRODUCTS . " where products_id = '" . (int)$pos_array['products_id'] . "'"));
          $products_new_quantity = (int)$products_quantity['products_quantity'] - $quantity_difference;
          $products_ordered = 0;
          if ($pos_array['products_quantity'] == 0) {
            $products_ordered = 1;
          }
          tep_db_query("update " . TABLE_PRODUCTS . " set products_quantity = " . $products_new_quantity . ", products_ordered = products_ordered + " . (int)$products_ordered . " where products_id = '" . (int)$pos_array['products_id'] . "'");
      }
      //UPDATE_INVENTORY_QUANTITY_END##############################################################################################################
 if($products_details["qty"] > 0)
      {
//check for qty pricing.
  if ($products_details['qty'] >= 1){
      $special_price = tep_get_products_special_price($pos_products_id, $customer_id);
      if ( (isset($special_price )) && ($special_price > 0)) {
        $p_products_price = $special['specials_new_products_price'];
       } else {
        $p_products_price = $products_details['final_price'];

       }
// compare final price to computed price if differnet then use computed price.
        if ($products_details['price'] != $p_products_price){
          $products_details['price'] = $p_products_price ;
        }
    } // end qtycheck

//if price is 0, get the regular price
if($p_products_price == 0){
    $product_price_query = tep_db_query("select products_price from " . TABLE_PRODUCTS . " where products_id = '" . $pos_array['products_id'] . "'");
    if (tep_db_num_rows($product_price_query)) {
      $product_price = tep_db_fetch_array($product_price_query);
    $p_products_price = $product_price['products_price'];
    }
}
/*
//get product per order
 $attributes_order_query = tep_db_query("select poa.options_values_price, poa.price_prefix FROM
  " . TABLE_POS_PRODUCTS_ATTRIBUTES . " poa
   WHERE
  pos_id = '" . $posID . "'
  and pos_products_id= '" . $pos_products_id. "'");
   $AddedOptionsPrice = '';
while($attributes_order = tep_db_fetch_array($attributes_order_query)){
           if ($attributes_order['price_prefix'] == '+'){
              $AddedOptionsPrice += $attributes_order['options_values_price'];
             }else{
              $AddedOptionsPrice -= $attributes_order['options_values_price'];
            }
   }
*/
          $Query = "update " . TABLE_POS_PRODUCTS . " set
                   products_model = '" . $products_details['model'] . "',
                   products_name = '" . str_replace("'", "&#39;", $products_details['name']) . "',
                   products_price = '" . $products_details['products_price'] . "',
                   final_price = '" . $products_details['final_price']. "',
                   products_tax = '" . $products_details['tax'] . "',
                   products_quantity = '" . $products_details['qty'] . "'
          where pos_products_id = '$pos_products_id'
          and pos_id = '$posID'  ;";

        tep_db_query($Query);
        // Update Tax and Subtotals
        $RunningSubTotal += $products_details["qty"] * $products_details['final_price'];
        $RunningTax += (($products_details["tax"]/100) * ($products_details['qty'] * $products_details['final_price']));
      }
      else
      {
        // 0 Quantity = Delete
        $Query = "delete from " . TABLE_POS_PRODUCTS . " where products_id = '$pos_products_id' and pos_id = '$posID';";
        tep_db_query($Query);
        $Query = "delete from " . TABLE_POS_PRODUCTS_ATTRIBUTES . " where products_id = '$pos_products_id'  and pos_id = '$posID;";
        tep_db_query($Query);
      }
    }
  }//end empty
  echo '$RunningSubTotal' . $RunningSubTotal;
    // Shipping Tax
      foreach($update_totals as $total_index => $total_details)
      {


        extract($total_details,EXTR_PREFIX_ALL,"ot");
// echo 'ot_totals';
// print_r($total_details);
        if($ot_code == "ot_shipping")
        {
          $RunningTax += (($AddShippingTax / 100) * $ot_value);
        }
      }

    // Update Totals

      $RunningTotal = 0;
      $sort_order = 0;

      // Do pre-check for Tax field existence
        $ot_tax_found = 0;
        foreach($update_totals as $total_details)
        {
          extract($total_details,EXTR_PREFIX_ALL,"ot");
          if($ot_code == "ot_tax")
          {
            $ot_tax_found = 1;
            break;
          }
        }

      foreach($update_totals as $total_index => $total_details)
      {
        extract($total_details,EXTR_PREFIX_ALL,"ot");

       if (($ot_tax_found == 0) && ($RunningTax > 0)) {
              //check to see if it's  an admin order or catalog order
                $pos_type = 0;

                $ot_total_tax_exists = 0;
                   $totals_query_5= tep_db_query("select code as ot_code_1  from " . TABLE_POS_TOTAL . " where pos_id = '" . $posID . "' and code = 'ot_tax'");
          while ($totals_5 = tep_db_fetch_array($totals_query_5)) {
                    $ot_total_tax_exists = 1;
           }


            //get ot_total from db since that does not exist yet.

          $totals_query_4= tep_db_query("select code as ot_code_1, sort_order  from " . TABLE_POS_TOTAL . " where pos_id = '" . $posID . "' and code = 'ot_total' order by sort_order");
          while ($totals_4 = tep_db_fetch_array($totals_query_4)) {
                 $sort_order_tmp_4 = $totals_4['sort_order'];
                 $ot_code_tmp_4 = $totals_4['ot_code_1'];
          }

           if ( ($ot_code_tmp_4 == "ot_total" ) && ($sort_order_tmp_4 <= 5)) {
               $sort_order_tmp = $sort_order_tmp_4 - 1;
               } else {
               $sort_order_tmp = MODULE_ORDER_TOTAL_TAX_SORT_ORDER ;
               }
          $ot_title_tax = 'Tax';
          $ot_text_tax = $currencies->format($RunningTax, 1, $pos_order->info['currency'], $pos_order->info['currency_value']);
          $ot_value_tax = $RunningTax;
          $ot_code_tax = 'ot_tax';
          $sort_order_tax = $sort_order_tmp;

            if ($ot_total_tax_exists == 0){
              $Query = "insert into " . TABLE_POS_TOTAL . " set
                        pos_id = '$posID',
                        title = '$ot_title_tax',
                        text = '$ot_text_tax',
                        value = '$ot_value_tax',
                        code = '$ot_code_tax',
                        sort_order = '$sort_order_tax'";
                      tep_db_query($Query);

             $ot_tax_found = 1;
           }
      }
        if( trim(strtolower($ot_title)) == "tax" || trim(strtolower($ot_title)) == "tax:" )
        {
          if($ot_code != "ot_tax" && $ot_tax_found == 0)
          {
            // Inserting Tax
            $ot_code = "ot_tax";
            $ot_value = "x"; // This gets updated in the next step
            $ot_tax_found = 1;
          }
        }

        if( trim($ot_title) && trim($ot_value) )
        {
          $sort_order++;

          // Update ot_subtotal, ot_tax, and ot_total codees
            if($ot_code == "ot_subtotal")
            $ot_value = $RunningSubTotal;

            if($ot_code == "ot_tax")
            {
            $ot_value = $RunningTax;
            // print "ot_value = $ot_value<br>\n";
            }
//discount
echo '$RunningTotal ' . $RunningTotal ;
     // CWS EDIT (start) -- Check for existence of subtotals...
            if($ot_code == "ot_total")
                        {

                $ot_value = $RunningTotal ;
                            if ( !$ot_subtotal_found )
                            {
                                // There was no subtotal on this order, lets add the running subtotal in.
                                $ot_value = $ot_value + $RunningSubTotal;
                            }
                        }
     // CWS EDIT (end) -- Check for existence of subtotals...

          // Set $ot_text (display-formatted value)
            // $ot_text = "\$" . number_format($ot_value, 2, '.', ',');

            $pos_order = new pos($posID);
            $ot_text = $currencies->format($ot_value, 1, $pos_order->info['currency'], $pos_order->info['currency_value']);

            if($ot_code == "ot_total")
            $ot_text = "<b>" . $ot_text . "</b>";

          if($ot_total_id > 0)
          {
            // In Database Already - Update
            $Query = "update " . TABLE_POS_TOTAL . " set
              title = '$ot_title',
              text = '$ot_text',
              value = '$ot_value',
              sort_order = '$sort_order'
              where pos_total_id = '$ot_total_id'";
            tep_db_query($Query);
          }
          else
          {
            // New Insert
            $Query = "insert into " . TABLE_POS_TOTAL . " set
              pos_id = '$posID',
              title = '$ot_title',
              text = '$ot_text',
              value = '$ot_value',
              code = '$ot_code',
              sort_order = '$sort_order'";
            tep_db_query($Query);
          }
          if ($ot_code == "ot_shipping" || $ot_code == "ot_lev_discount" || $ot_code == "ot_custom" || $ot_code == "ot_cod_fee") {
            // Again, because products are calculated in terms of default currency, we need to align shipping, custom etc. values with default currency
            $RunningTotal += $ot_value / $pos_order->info['currency_value'];
          } else if($ot_code == "ot_coupon" ||$ot_code == "ot_customer_discount" || $ot_code == "ot_discount" || $ot_code == "ot_gv"){
          //subtract discounts
          $RunningTotal -= $ot_value / $pos_order->info['currency_value'];
          }else{
          //add to gether all other ot-totals
            if($ot_code != "ot_tax") {
               $RunningTotal += $ot_value / $pos_order->info['currency_value'] ;
            }else if (($ot_code == "ot_tax") && (DISPLAY_PRICE_WITH_TAX == true) ) {
            //add tax to ot-total
               $RunningTotal += $ot_value / $pos_order->info['currency_value'];
            }
          }
        }
      elseif (($ot_total_id > 0) && ($ot_code != "ot_shipping")) { // Delete Total Piece

          // Delete Total Piece
          $Query = "delete from " . TABLE_POS_TOTAL . " where pos_total_id = '$ot_total_id'";
          tep_db_query($Query);
        }

      }
    }
        if ($pos_updated == 1) {
       //  $messageStack->add_session('search', SUCCESS_ORDER_UPDATED, 'success');
        } else {
          $messageStack->add_session('search', WARNING_ORDER_NOT_UPDATED, 'warning');
        }
 //teoteo
 //    tep_redirect(tep_href_link(FILENAME_CREATE_POS_ORDERS, 'posID=' . $posID . '&action=edit'));

  break;

//********************* add product *************
  // Add a Product
  case 'add_product_new':

            // Get Order Info
//$customer_id = $pos_order->customer['id'];
       //  $pos_order = new pos($posID);
// sub_products_qty

   if (isset($_GET["add_product_options"])) {
      $add_product_options = $_GET["add_product_options"] ;
    }else if (isset($_POST["add_product_options"])){
      $add_product_options = $_POST["add_product_options"] ;
    } else {
      $add_product_options = array() ;
   }
         $add_product_products_id = (isset($_POST['products_id']) ? $_POST['products_id'] : '');
         $add_product_quantity = (isset($_POST['cart_quantity']) ? $_POST['cart_quantity'] : '');
         $customer_id = (isset($pos_order->customer['id']) ? $pos_order->customer['id'] : '');

// intialize for first attribute
         $AddedOptionsPrice = 0;
         $option_is_text = 0;
         $item_has_down = 0;
         $query_products_options_values = '';
         $products_options_values= '';

  $customer_id = $pos_order->customer['id'];
   //gets customer group ID
  if($customer_id == "") {
    $customer_group_id="G";
  } else {
    $getcustomer_GroupID_query = tep_db_query("select customers_group_id   from " . TABLE_CUSTOMERS . " where  customers_id = '" . (int)$customer_id . "'");
    $getcustomer_GroupID = tep_db_fetch_array($getcustomer_GroupID_query);
    $customer_group_id=$getcustomer_GroupID['customers_group_id'];
  }

    // Get Product Info
    $product_info_guery_1 = tep_db_query("select
     pd.products_name,
     p.products_id,
     p.products_price,
     p.products_tax_class_id,
     p.products_model,
     p.products_status
    FROM " . TABLE_PRODUCTS . " p,
         " . TABLE_PRODUCTS_DESCRIPTION . " pd
    where
     p.products_id = '" . $add_product_products_id . "'
     and pd.products_id = p.products_id
     and pd.language_id = '" . (int)$languages_id . "'
    ");
            while ($product_info_array = tep_db_fetch_array($product_info_guery_1)){
                    $add_products_model = $product_info_array['products_model'];
                    $add_products_name = $product_info_array['products_name'] ;
                    $add_products_price = $product_info_array['products_price'];
                    $products_tax_class_id = $product_info_array['products_tax_class_id'];
                }

     $special_price = tep_get_products_special_price($add_product_products_id, $customer_id);

      if ( (isset($special_price )) && ($special_price > 0)) {
             $p_products_price = $special_price;
       }

//if price is 0, get the regular price
if($p_products_price == 0){
    $product_price_query = tep_db_query("select products_price from " . TABLE_PRODUCTS . " where products_id = '" . $add_product_products_id . "'");
    if (tep_db_num_rows($product_price_query)) {
      $product_price = tep_db_fetch_array($product_price_query);
    $p_products_price = $product_price['products_price'];
    }
}
      // Following functions are defined at the bottom of this file
      $CountryID = tep_get_country_id($pos_order->delivery["country"]);
      $ZoneID = tep_get_zone_id($CountryID, $pos_order->delivery["state"]);

      $ProductsTax = tep_get_tax_rate($products_tax_class_id, $CountryID, $ZoneID);

                $Query = "insert into " . TABLE_POS_PRODUCTS . " set
                pos_id = '" . $posID . "',
                products_id = '" . $add_product_products_id . "',
                products_model = '" . $add_products_model . "',
                products_name = '" . str_replace("'", "&#39;", $add_products_name) . "',
                products_price = '" . $p_products_price . "',
                final_price = '" . ($p_products_price + $AddedOptionsPrice) . "',
                products_tax = '" . $ProductsTax . "',
                products_quantity = '" . $add_product_quantity . "'";
            tep_db_query($Query);
            $new_product_id = tep_db_insert_id();

  // Get Product Attribute Info

   if( (isset($add_product_options)) && (is_array($add_product_options)) ){

foreach($add_product_options as $attri_id => $attri_option_value_id){

//check for sub product
    $products_id_tmp = $add_product_products_id;
      if(tep_subproducts_parent($products_id_tmp)){
          $products_id_query = tep_subproducts_parent($products_id_tmp);
        }else{
          $products_id_query = $products_id_tmp;
        }


//get option type:
$attributes_type_query = tep_db_query("select pa.products_attributes_id, pa.options_values_id, po.options_type
          FROM
          " . TABLE_PRODUCTS_ATTRIBUTES . " pa,
          " . TABLE_PRODUCTS_OPTIONS . " po
        WHERE
        pa.products_id = '" . $products_id_query . "'
        and pa.products_attributes_id = '" . $attri_id. "'
        and po.products_options_id = pa.options_id" );

while ($attributes_type = tep_db_fetch_array($attributes_type_query)){
        $attributes_options_type = $attributes_type['options_type'];
}

// if drop down select
if($attributes_options_type == 0){
$attri_id = $attri_option_value_id ;
}

//echo '$attri_id ' . $attri_id ;
//echo  '$attri_option_value_id ' . $attri_option_value_id;
////echo '$attributes_options_type ' . $attributes_options_type;
//echo '<br>';

//check box array
if (is_array($attri_option_value_id)){
    $attri_option_value_id_tmp = $attri_option_value_id ;
   foreach($attri_option_value_id_tmp as $attri_option_id_tmp => $attri_option_value_id_tmp){
    $attri_option_id = $attri_option_id_tmp ;
    $attri_option_value_id= $attri_option_value_id_tmp;
   }

unset($attri_option_value_id);
 }
//intialize values
$option_is_text = 0;
$item_has_down = 0;
$query_products_options_values = '';
$products_options_values= '';


// check to see if there is a downloadable file
  if (DOWNLOAD_ENABLED ==  1) {
//check for downloads
     $attributes_download_query = tep_db_query("select pa.products_attributes_id, poval.products_options_values_id, pa.products_id, pa.options_id, pa.options_values_id, pa.options_values_price, pa.price_prefix, poptt.products_options_name, poval.products_options_values_name, pad.products_attributes_filename, pad.products_attributes_maxdays, pad.products_attributes_maxcount  FROM
     " . TABLE_PRODUCTS_OPTIONS . " popt,
     " . TABLE_PRODUCTS_OPTIONS_TEXT . " poptt,
     " . TABLE_PRODUCTS_OPTIONS_VALUES . " poval,
     " . TABLE_PRODUCTS_ATTRIBUTES . " pa,
     " . TABLE_PRODUCTS_ATTRIBUTES_DOWNLOAD . " pad
   WHERE
    pa.products_id = '" . $products_id_query . "'
    and pa.products_attributes_id = '" . $attri_id. "'
    and poptt.products_options_text_id = pa.options_id
    and poval.products_options_values_id =  pa.options_values_id
    and poptt.language_id = '" . (int)$languages_id . "'
    and poval.language_id = '" . (int)$languages_id . "'
   and pad.products_attributes_id = pa.products_attributes_id
  order by pa.products_options_sort_order
    limit 1");

 if (tep_db_num_rows($attributes_download_query)) {
   $item_has_down = '1';
   } // end if tep_db_num_rows
}// end if DOWNLOAD_ENABLED

//check for text
  if($attributes_options_type != '1' && $attributes_options_type != '4'){
    $query_products_options_values = $attri_option_value_id ;
    $option_is_text = 0 ;
  }else{
    if (!empty($attri_option_value_id)){
     $query_products_options_values = 0;
     $option_is_text = 1 ;
   }
  }

// get attibutes data
if ($item_has_down == '1')  {
  $attributes_query = tep_db_query("select pa.products_attributes_id, poval.products_options_values_id, pa.products_id, pa.options_id, pa.options_values_id, pa.options_values_price, pa.price_prefix, poptt.products_options_name, poval.products_options_values_name, pad.products_attributes_filename, pad.products_attributes_maxdays, pad.products_attributes_maxcount  FROM
  " . TABLE_PRODUCTS_OPTIONS . " popt,
  " . TABLE_PRODUCTS_OPTIONS_TEXT . " poptt,
  " . TABLE_PRODUCTS_OPTIONS_VALUES . " poval,
  " . TABLE_PRODUCTS_ATTRIBUTES . " pa,
  " . TABLE_PRODUCTS_ATTRIBUTES_DOWNLOAD . " pad
   WHERE
    pa.products_id = '" . $products_id_query . "'
    and pa.products_attributes_id = '" . $attri_id. "'
  and poptt.products_options_text_id = pa.options_id
  and poval.products_options_values_id =  pa.options_values_id
  and poptt.language_id = '" . (int)$languages_id . "'
  and poval.language_id = '" . (int)$languages_id . "'
  and pad.products_attributes_id = pa.products_attributes_id
  order by pa.products_options_sort_order
  limit 1
  ");
} else if (($item_has_down == '0') && ($option_is_text == 0)){
  $attributes_query = tep_db_query("select pa.products_attributes_id, poval.products_options_values_id, pa.products_id, pa.options_id, pa.options_values_id, pa.options_values_price, pa.price_prefix, poptt.products_options_name, poval.products_options_values_name from
  " . TABLE_PRODUCTS_OPTIONS . " popt,
  " . TABLE_PRODUCTS_OPTIONS_TEXT . " poptt,
  " . TABLE_PRODUCTS_OPTIONS_VALUES . " poval,
  " . TABLE_PRODUCTS_ATTRIBUTES . " pa
where
    pa.products_id = '" . $products_id_query . "'
    and pa.products_attributes_id = '" . $attri_id. "'
  and poptt.products_options_text_id = pa.options_id
  and poval.products_options_values_id =  pa.options_values_id
  and poptt.language_id = '" . (int)$languages_id . "'
  and poval.language_id = '" . (int)$languages_id . "'
  order by pa.products_options_sort_order
  limit 1
  ");
   } else if ($option_is_text == 1){
  $attributes_query = tep_db_query("select poval.products_options_values_id, pa.products_attributes_id, pa.products_id, pa.options_id, pa.options_values_id, pa.options_values_price, pa.price_prefix, poptt.products_options_name  FROM
  " . TABLE_PRODUCTS_OPTIONS . " popt,
  " . TABLE_PRODUCTS_OPTIONS_TEXT . " poptt,
  " . TABLE_PRODUCTS_OPTIONS_VALUES . " poval,
  " . TABLE_PRODUCTS_ATTRIBUTES . " pa
   WHERE
    pa.products_id = '" . $products_id_query . "'
    and pa.products_attributes_id = '" . $attri_id. "'
  and poptt.products_options_text_id = pa.options_id
  and poptt.language_id = '" . (int)$languages_id . "'
  order by pa.products_options_sort_order
  limit 1
  ");
    }

while  ($attributes = tep_db_fetch_array($attributes_query)){
  if($option_is_text == 1){
    $products_options_values = htmlentities(stripslashes($attri_option_value_id), ENT_QUOTES);
   }else{
    $products_options_values = $attributes['products_options_values_name'];
  }

  $pos_products_id = $new_product_id;
  $products_options = $attributes['products_options_name'];
  $options_values_price = $attributes['options_values_price'];
  $price_prefix = $attributes['price_prefix'];
  $products_options_id = $attributes['options_id'];
  $products_options_values_id = $attributes['options_values_id'];

//downloads
if ($item_has_down == '1') {
$poss_products_filename = $attributes['products_attributes_filename'];
$download_maxdays = $attributes['products_attributes_maxdays'];
$download_count = $attributes['products_attributes_maxcount'];
    }

//add attibute price to product price
 if ($price_prefix == '+'){
   $AddedOptionsPrice += $options_values_price;
  }else{
   $AddedOptionsPrice -= $options_values_price;
  }

// update final price
$product_price_update = tep_db_query("select final_price from
                             " . TABLE_POS_PRODUCTS . "
                            where
                            pos_id = $posID and
                            pos_products_id = $pos_products_id ");
  while  ($product_price_array_1 = tep_db_fetch_array($product_price_update)){
    $Query_final_price = "update " . TABLE_POS_PRODUCTS . " set
     final_price = '" . ($p_products_price + $AddedOptionsPrice) . "'
  where pos_id = $posID and pos_products_id = $pos_products_id ";
   tep_db_query($Query_final_price);
 }

if(isset($add_product_options)){
   $Query = "insert into " . TABLE_POS_PRODUCTS_ATTRIBUTES . " set
               pos_id = $posID,
               pos_products_id = $pos_products_id,
               products_options = '". $products_options . "',
               products_options_values = '" . $products_options_values . "',
               options_values_price = $options_values_price,
               price_prefix = '" . $price_prefix . "',
               products_options_id = '" . $products_options_id . "' ,
               products_options_values_id =  '" . $products_options_values_id . "' ";

  tep_db_query($Query);
 }

// add download insert
          if ((DOWNLOAD_ENABLED ==  1) && isset($poss_products_filename) && tep_not_null($poss_products_filename)) {
            $sql_data_array = array('pos_id' => $posID,
                                    'pos_products_id' => $new_product_id,
                                    'orders_products_filename' => $poss_products_filename,
                                    'download_maxdays' => $download_maxdays,
                                    'download_count' => $download_count);
            tep_db_perform(TABLE_POS_PRODUCTS_DOWNLOAD, $sql_data_array);
           }


       } // end while get option
     }
    }

  //UPDATE_INVENTORY_QUANTITY_START##############################################################################################################
  tep_db_query("update " . TABLE_PRODUCTS . " set products_quantity = products_quantity - " . $add_product_quantity . ", products_ordered = products_ordered + " . $add_product_quantity . " where products_id = '" . $products_id_query . "'");
  //UPDATE_INVENTORY_QUANTITY_END##############################################################################################################
//end add product info

//get total of product using order code
      $pos_order = new pos($posID);
      $RunningSubTotal = 0;
      $RunningTax = 0;

      for ($i=0; $i<sizeof($pos_order->products); $i++)
      {
      $RunningSubTotal += ($pos_order->products[$i]['qty'] * $pos_order->products[$i]['final_price']);
     //tax caculated on a per product bases
      $RunningTax += (($pos_order->products[$i]['tax'] / 100) * ($pos_order->products[$i]['qty'] * $pos_order->products[$i]['final_price']));
      }
    //subtotal to total
       $RunningTotal = $RunningSubTotal;
    //add tax
       $RunningTotal += $RunningTax;

      // Calculate Tax and Sub-Totals
   // get exsisting order details so we can update them

            $totals_query_5 = tep_db_query("select title as ot_title, text as ot_text, value as ot_value, code as ot_code  from " . TABLE_POS_TOTAL . " where pos_id = '" . $posID . "' order by sort_order");
          while ($totals_1 = tep_db_fetch_array($totals_query_5)) {
            $totals_array[] = array('ot_title' => $totals_1['ot_title'],
                              'ot_text' => $totals_1['ot_text'],
                              'ot_value' => $totals_1['ot_value'],
                              'ot_code' => $totals_1['ot_code']);
          }

//parse array
  for ($i=0, $n=sizeof($totals_array); $i<$n; $i++) {
    $totals = $totals_array[$i];

       //add other order totals
          if ($totals['ot_code'] == "ot_shipping" || $totals['ot_code'] == "ot_custom" || $totals['ot_code'] == "ot_cod_fee") {
            // add ot_ items not already added
            $RunningTotal += $totals['ot_value'] / $pos_order->info['currency_value'];
       //deduct discounts
           } else if($totals['ot_code'] == "ot_coupon" ||$totals['ot_code'] == "ot_customer_discount" || $totals['ot_code'] == "ot_discount" || $totals['ot_code'] == "ot_gv" || $totals['ot_code'] == "ot_lev_discount" ){
          //subtract discounts
          $RunningTotal -= $totals['ot_value'] / $pos_order->info['currency_value'];
          }

      // Tax
      $Query = "update " . TABLE_POS_TOTAL . " set
        text = '\$" . number_format($RunningTax, 2, '.', ',') . "',
        value = '" . $RunningTax . "'
        where code='ot_tax' and pos_id=$posID";
      tep_db_query($Query);

      // Sub-Total
      $Query = "update " . TABLE_POS_TOTAL . " set
        text = '\$" . number_format($RunningSubTotal, 2, '.', ',') . "',
        value = '" . $RunningSubTotal . "'
        where code='ot_subtotal' and pos_id=$posID";
      tep_db_query($Query);

      $Query = "update " . TABLE_POS_TOTAL . " set
        text = '<b>\$" . number_format($RunningTotal, 2, '.', ',') . "</b>',
        value = '" . $RunningTotal . "'
        where code='ot_total' and pos_id=$posID";
      tep_db_query($Query);

  } //end of for
    tep_redirect(tep_href_link(FILENAME_CREATE_POS_ORDERS, 'posID=' . $posID . '&action=edit'));

 break;
 case 'update_totals':
  define('AJAX_NOW', 'yes');

$_POST['gv_redeem_code']=$gc;

if($cb==1){
tep_session_register('cot_gv');
}else{tep_session_unregister('cot_gv');}
$_POST['i']=2;
 $shipping['title']=$zship.'';
 if (tep_not_null($_POST['tip'])) {
    $tip = tep_db_prepare_input($_POST['tip']);
  }

// load the selected payment module
  require(DIR_WS_CLASSES . 'pos_payment.php');
  if ($credit_covers) $payment=''; //rmh M-S_ccgv
  $payment_modules = new payment($payment);
  require(DIR_WS_CLASSES . 'pos_order_total.php'); //rmh M-S_ccgv
  $shipping['cost']=$tip;

  require(DIR_WS_CLASSES . 'pos_order.php');
  $pos_order = new order;

  $payment_modules->update_status();
  $pos_total_modules = new order_total; //rmh M-S_ccgv

  if (is_array($payment_modules->modules)) {
    $payment_modules->pre_confirmation_check();
  }

// load the selected shipping module
  require(DIR_WS_CLASSES . 'pos_shipping.php');
  $shipping_modules = new shipping($shipping);

 if (MODULE_ORDER_TOTAL_INSTALLED) {
    $pos_total_modules->process();

//    echo $pos_total_modules->output() ;

  }
    tep_redirect(tep_href_link(FILENAME_CREATE_POS_ORDERS, 'posID=' . $posID . '&action=edit'));

  break;
// Remove CVV Number
    case 'deleteccinfo':
      $posID = (int)$posID;
      tep_db_query("update " . TABLE_POS . " set cc_ccv = null where pos_id = '" . tep_db_input($posID) . "'");
      tep_db_query("update " . TABLE_POS . " set cc_number = '0000000000000000' where pos_id = '" . tep_db_input($posID) . "'");
      tep_db_query("update " . TABLE_POS . " set cc_expires = null where pos_id = '" . tep_db_input($posID) . "'");
      tep_db_query("update " . TABLE_POS . " set cc_start = null where pos_id = '" . tep_db_input($posID) . "'");
      tep_db_query("update " . TABLE_POS . " set cc_issue = null where pos_id = '" . tep_db_input($posID) . "'");

      tep_redirect(tep_href_link(FILENAME_CREATE_POS_ORDERS, 'posID=' . $posID . '&action=edit'));
      break;

    } //end action
  }// end action NUll

  if (($action == 'edit') && isset($posID)) {


    $posID = (int)$posID;

    $poss_query = tep_db_query("select pos_id from " . TABLE_POS . " where pos_id = '" . (int)$posID . "'");
    $pos_exists = 1;
    if (!tep_db_num_rows($poss_query)) {
      $pos_exists = 0;
   //   $messageStack->add('search', sprintf(ERROR_ORDER_DOES_NOT_EXIST, $posID), 'error');
    }
  }
//pre page display code

require_once('includes/classes/http_client.php');
require_once('includes/classes/pos_cart.php');
require_once(DIR_WS_LANGUAGES . $language . '/' . 'pos_order.php');

require_once(DIR_WS_CLASSES . 'pos_order.php');
$pos_order = new pos;

//the next 4 lines are for ccgv
// require(DIR_WS_CLASSES . 'pos_order_total.php');


require_once(DIR_WS_CLASSES . 'pos_payment.php');
$payment_modules = new pos_payment;


$cart = new posCart;
 $total_weight = $cart->show_weight();
 $total_count = $cart->count_contents();


require_once(DIR_WS_CLASSES . 'pos_shipping.php');
$shipping_modules = new shipping;
// if there is nothing in the customers cart, redirect them to the shopping cart page
//$cart_contents_count = '' ;
//$cart_contents_count = @$cart->count_contents();

 if(!class_exists('pos_order_total')) {
  include(DIR_WS_CLASSES . 'pos_order_total.php');
  }
  $pos_order_total_modules = new pos_order_total;
  $pos_order_total_modules->collect_posts();
  $pos_order_total_modules->pre_confirmation_check();

// register a random ID in the session to check throughout the checkout procedure
// against alterations in the shopping cart contents
if (!tep_session_is_registered('posID')) tep_session_register('posID');
//$posID = $cart->posID;


// if no shipping destination address was selected, use the customers own address as default
if (!tep_session_is_registered('sendto')) {
tep_session_register('sendto');
$sendto = $customer_default_address_id;
} else {
// verify the selected shipping address
$check_address_query = tep_db_query("select count(*) as total from " . TABLE_ADDRESS_BOOK . " where customers_id = '" . (int)$customer_id . "' and address_book_id = '" . (int)$sendto . "'");
$check_address = tep_db_fetch_array($check_address_query);

if ($check_address['total'] != '1') {
$sendto = $customer_default_address_id;
if (tep_session_is_registered('shipping')) tep_session_unregister('shipping');
}
}
// if no billing destination address was selected, use the customers own address as default
if (!tep_session_is_registered('billto')) {
tep_session_register('billto');
$billto = $customer_default_address_id;
} else {
// verify the selected billing address
$check_address_query = tep_db_query("select count(*) as total from " . TABLE_ADDRESS_BOOK . " where customers_id = '" . (int)$customer_id . "' and address_book_id = '" . (int)$billto . "'");
$check_address = tep_db_fetch_array($check_address_query);

if ($check_address['total'] != '1') {
$billto = $customer_default_address_id;
if (tep_session_is_registered('payment')) tep_session_unregister('payment');
}
}

// if the order contains only virtual products, forward the customer to the billing page as
// a shipping address is not needed
if ($pos_order->content_type == 'virtual') {
if (!tep_session_is_registered('shipping')) tep_session_register('shipping');
$shipping = false;
$sendto = false;
tep_redirect(tep_href_link(FILENAME_CHECKOUT_PAYMENT, '', 'SSL'));
}
tep_session_unregister('billing');
tep_session_unregister('payment');
if (isset($_POST['payment'])) $payment = $_POST['payment'];
if (!tep_session_is_registered('payment')) tep_session_register('payment');



if($n==1){

if (isset($_POST['save_x'])){
$paynow=3;
}
if (isset($_POST['preview_x'])){
$paynow=5;
}


//i commented this out so payment is not required in this page and total can be accessed
/*if ( ( is_array($payment_modules->modules) && (sizeof($payment_modules->modules) > 1) && !is_object($$payment) ) || (is_object($$payment) && ($$payment->enabled == false)) ) {
tep_redirect(tep_href_link(FILENAME_CHECKOUT_SHIPPING, 'error_message=' . urlencode(ERROR_NO_PAYMENT_MODULE_SELECTED), 'SSL'));
*/
tep_session_unregister('payment');
$payment_modules->update_status();
}
if (is_array($payment_modules->modules)) {
$payment_modules->pre_confirmation_check();
}
//}
while (list($key, $value) = each($_POST))
{
tep_session_register($key);
}
if ( defined('MODULE_ORDER_TOTAL_SHIPPING_FREE_SHIPPING') && (MODULE_ORDER_TOTAL_SHIPPING_FREE_SHIPPING == 'true') ) {
$pass = false;

switch (MODULE_ORDER_TOTAL_SHIPPING_DESTINATION) {
case 'national':
if ($pos_order->delivery['country_id'] == STORE_COUNTRY) {
$pass = true;
}
break;
case 'international':
if ($pos_order->delivery['country_id'] != STORE_COUNTRY) {
$pass = true;
}
break;
case 'both':
$pass = true;
break;
}

// echo 'order total ' . $pos_order->info['total'];
// print_r($pos_order->info) ;
// print_r($pos_order->products) ;

$free_shipping = false;
if ( ($pass == true) && ($pos_order->info['total'] >= MODULE_ORDER_TOTAL_SHIPPING_FREE_SHIPPING_OVER) ) {
$free_shipping = true;

include(DIR_WS_LANGUAGES . $language . '/modules/order_total/ot_shipping.php');
}
} else {
$free_shipping = false;
}

// process the selected shipping method
if ( isset($_POST['action']) && ($_POST['action'] == 'process') ) {
$comments = tep_db_prepare_input($_POST['comments']);

if ( (tep_count_shipping_modules() > 0) || ($free_shipping == true) ) {
if ( (isset($_POST['shipping'])) && (strpos($_POST['shipping'], '_')) ) {
$shipping = $_POST['shipping'];

list($module, $method) = explode('_', $shipping);
if ( is_object($$module) || ($shipping == 'free_free') ) {
if ($shipping == 'free_free') {
$quote[0]['methods'][0]['title'] = FREE_SHIPPING_TITLE;
$quote[0]['methods'][0]['cost'] = '0';
} else {
$quote = $shipping_modules->quote($method, $module);
}
if (isset($quote['error'])) {
} else {
if ( (isset($quote[0]['methods'][0]['title'])) && (isset($quote[0]['methods'][0]['cost'])) ) {
$shipping = array('id' => $shipping,
'title' => (($free_shipping == true) ? $quote[0]['methods'][0]['title'] : $quote[0]['module'] . ' (' . $quote[0]['methods'][0]['title'] . ')'),
'cost' => $quote[0]['methods'][0]['cost']);

tep_redirect(tep_href_link(FILENAME_CHECKOUT_CONFIRMATION,'paynow='.$paynow, 'SSL'));
}
}
}
}
} else {
$shipping = false;

tep_redirect(tep_href_link(FILENAME_CHECKOUT_CONFIRMATION, 'paynow='.$paynow, 'SSL'));
}
}

// get all available shipping quotes
$quotes = $shipping_modules->quote();

// if no shipping method has been selected, automatically select the cheapest method.
// if the modules status was changed when none were available, to save on implementing
// a javascript force-selection method, also automatically select the cheapest shipping
// method if more than one module is now enabled
if ( !tep_session_is_registered('shipping') || ( tep_session_is_registered('shipping') && ($shipping == false) && (tep_count_shipping_modules() > 1) ) ) $shipping = $shipping_modules->cheapest();
require(DIR_WS_LANGUAGES . $language . '/' . 'pos_checkout_shipping.php');
require(DIR_WS_LANGUAGES . $language . '/' . 'pos_checkout_payment.php');


?>
<!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN">
<html <?php echo HTML_PARAMS; ?>>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=<?php echo CHARSET; ?>">
<title><?php echo TITLE; ?></title>
<link rel="stylesheet" type="text/css" href="includes/stylesheet.css">
<script type="text/javascript" src="includes/menu.js"></script>
<script type="text/javascript" src="includes/general.js"></script>
<script type="text/javascript" src="includes/javascript/global.js"></script>

<?php //echo $payment_modules->javascript_validation(); ?>
<script type="text/javascript"><!--
function ajaxLoader(url,id) {

  if (document.getElementById) {
    var x = (window.ActiveXObject) ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
  }
  if (x) {

    x.onreadystatechange = function() {
	document.getElementById("contentLYR").innerHTML ='<img style="vertical-align:middle" src="images/loading.gif">Loading, please wait...' ;
      if (x.readyState == 4 && x.status == 200) {
        el = document.getElementById(id);
   el.innerHTML ="";
        el.innerHTML = x.responseText;

      }
    }
    x.open("GET", url, true);

    x.send(null);

  }
}
var selected;

var zhipper='<?php echo $shipping['title']; ?>';
var Csid='<?php echo $osCAdminID; ?>';
var zprice='<?php echo $shipping['cost']; ?>';
var selected;

function selectRowEfpos_ordert2(object, buttonSelect) {
  if (!selected) {
    if (document.getElementById) {
      selected = document.getElementById('defaultSelected');
    } else {
      selected = document.all['defaultSelected'];
    }
  }

  if (selected) selected.codeName = 'moduleRow';
  object.codeName = 'moduleRowSelected2';
  selected = object;

// one button is not an array
  if (document.checkout_payment.shipping[0]) {
    document.checkout_payment.shipping[buttonSelect].checked=true;
  } else {
    document.checkout_payment.shipping.checked=true;
  }
}

function rowOverEfpos_ordert(object) {
  if (object.codeName == 'moduleRow') object.codeName = 'moduleRowOver';
}

function rowOutEfpos_ordert(object) {
  if (object.codeName == 'moduleRowOver') object.codeName = 'moduleRow';
}
//--></script>
<script type="text/javascript"><!--
var selected;
<?php//rmh M-S_ccgv begin ?>
var submitter = null;
function submitFunction() {
   submitter = 1;
   }
<?php//rmh M-S_ccgv end ?>
function selectRowEfpos_ordert(object, buttonSelect) {
  if (!selected) {
    if (document.getElementById) {
      selected = document.getElementById('defaultSelected');
    } else {
      selected = document.all['defaultSelected'];
    }
  }

  if (selected) selected.codeName = 'moduleRow';
  object.codeName = 'moduleRowSelected';
  selected = object;

// one button is not an array
  if (document.checkout_payment.payment[0]) {
    document.checkout_payment.payment[buttonSelect].checked=true;
  } else {
    document.checkout_payment.payment.checked=true;
  }
}

function rowOverEfpos_ordert(object) {
  if (object.codeName == 'moduleRow') object.codeName = 'moduleRowOver';
}

function rowOutEfpos_ordert(object) {
  if (object.codeName == 'moduleRowOver') object.codeName = 'moduleRow';
}
//--></script>

<script type="text/javascript"><!--
function popupWindow(url) {
  window.open(url,'popupWindow','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,copyhistory=no,width=650,height=500,screenX=150,screenY=150,top=150,left=150')
}
//--></script>
</head>
<body bgcolor="#FFFFFF">
<!-- header //-->
<?php
  require(DIR_WS_INCLUDES . 'header.php');
?>
<!-- header_eof //-->

<!-- body //-->
<table border="0" width="100%" cellspacing="2" cellpadding="2">
  <tr>
    <td width="<?php echo BOX_WIDTH; ?>" valign="top"><table border="0" width="<?php echo BOX_WIDTH; ?>" cellspacing="1" cellpadding="1" code="columnLeft">
<!-- left_navigation //-->
<?php require(DIR_WS_INCLUDES . 'column_left.php'); ?>
<!-- left_navigation_eof //-->
    </table></td>
<!-- body_text //-->
    <td width="100%" valign="top"><table border="0" width="100%" cellspacing="0" cellpadding="2">
<?php

//   $pos_order = new pos($posID);

  if ($action == 'edit')  {
  if (tep_not_null($posID) ){
   $pos_order = new pos($posID);
   }
?>

<!-- Begin Addresses Block -->
      <tr>
  <td><table width="100%" border="0" cellspacing="0" cellpadding="2">
    <tr>
      <td valign="top">
      <!-- Customer Info Block -->
    <table border="0" cellspacing="0" cellpadding="2">
    <tr>
    <td colspan='2' class="main" valign="top">
    <?php echo 'Temp Order id: ' . $posID ; ?>
    </td>
    </tr>
    <tr>
 <?php if( (POS_SHOW_CUSTOMER_INFO == 'true')||(POS_SHOW_BILLING == 'true')||(POS_SHOW_SHIPPING == 'true') ) { ?>

 <td class="main" valign="top">

        <?php echo '<a href="' . tep_href_link(FILENAME_ADD_CUSTOMER, 'posID=' . $posID . '&action=addnew') . '">' . tep_image_button('button_add_customer.gif', 'Add Customer') . '&nbsp;</a>' ;?>
  </td>
  <td class="main" valign="top">
<?php
   echo tep_draw_form('update_customer', FILENAME_CREATE_POS_ORDERS, 'posID='.$posID . '&action=update_customer', 'post', '', 'SSL');
   echo tep_draw_input_field('update_customer_customers_id', $update_customer_customers_id);
;?>

<input type=button name=open_popup ONCLICK="window.open('<?php echo tep_href_link('treeview_pos_cust.php', tep_get_all_get_params(array('action', 'update_customer'))) ;?>', 'popuppage', 'scrollbars=yes,resizable=yes,menubar=yes,width=400,height=600'); " value="<?php echo 'Find Customer ID' ;?> ">
           <input type="hidden" name="posID" value="<?php echo $posID ;?>">
        <?php echo  tep_image_submit('button_update.gif', 'Update Customer info') ;?>

 </form>
 </td>
<?php } ?>
   <td class="main" valign="top">
<?php
 //    echo '<a href="' . tep_href_link(FILENAME_CREATE_POS_ORDERS, 'action=start_order') . '">' . tep_image_button('button_start_order.gif', 'Start a New Sale') . '&nbsp;</a>';
    echo tep_draw_form('start_order', FILENAME_CREATE_POS_ORDERS, tep_get_all_get_params(array('action','paycc'), 'post', '', 'SSL') . 'action=start_order', 'post', '', 'SSL');
   echo  tep_image_submit('button_update.gif', 'Start a New Sale');
?>
</form>
</td>
</tr>
    <tr>
   <?php

    echo tep_draw_form('update_order', FILENAME_CREATE_POS_ORDERS, tep_get_all_get_params(array('action','paycc'), 'post', '', 'SSL') . 'action=update_order', 'post', '', 'SSL');


   if( POS_SHOW_CUSTOMER_INFO == 'true' ){
	 ?>
    <td colspan='2' class="main" valign="top"><b><?php echo ENTRY_CUSTOMER; ?></b>  </td>
   <?php  }
   if(POS_SHOW_BILLING == 'true' ){
   ?>
    <td colspan='2' class="main" valign="top"><b><?php echo ENTRY_BILLING_ADDRESS; ?></b></td>
   <?php  }
   if(POS_SHOW_SHIPPING == 'true' ){
   ?>
    <td colspan='2' class="main" align="left"><b><?php echo ENTRY_SHIPPING_ADDRESS; ?></b></td>
   <?php  }
   ?>
    </tr>
    <tr>
        <?php
        if( POS_SHOW_CUSTOMER_INFO == 'true' ){
	    ?>

    <td colspan='2' class="main">
    <table border="0" cellspacing="0" cellpadding="2" code="infoBox">
      <tr>
      <td class="editOrder"><b><?php echo ENTRY_NAME ?></b></td>
      <td><input name='update_customer_name' size='37' value='<?php echo tep_html_quotes($pos_order->customer['name']); ?>'></td>
      </tr>
      <tr>
       <td  code="editOrder"><?php echo TABLE_HEADING_CUSTOMER_ID ?>  </td>
       <td><?php
       if ( $pos_order->customer['id'] == 0 ) {
       echo TABLE_HEADING_PWA ;
       }else{
       echo $pos_order->customer['id'];
       }
       ;?>  </td>
      </tr>
      <tr>
        <td class="editOrder"><b><?php echo ENTRY_COMPANY ?></b></td>
        <td><input name='update_customer_company' size='37' value='<?php echo tep_html_quotes($pos_order->customer['company']); ?>'></td>
      </tr>
      <tr>
        <td class="editOrder"><b><?php echo ENTRY_CUSTOMER_ADDRESS ?></b></td>
        <td><input name='update_customer_street_address' size='37' value='<?php echo tep_html_quotes($pos_order->customer['street_address']); ?>'></td>
      </tr>
      <tr>
        <td class="editOrder"><b><?php echo ENTRY_SUBURB ?></b></td>
        <td><input name='update_customer_suburb' size='37' value='<?php echo tep_html_quotes($pos_order->customer['suburb']); ?>'></td>
      </tr>
      <tr>
        <td class="editOrder"><b><?php echo ENTRY_CITY ?></b></td>
        <td><input name='update_customer_city' size='15' value='<?php echo tep_html_quotes($pos_order->customer['city']); ?>'> </td>
      </tr>
      <tr>
        <td class="editOrder"><b><?php echo ENTRY_STATE ?></b></td>
        <td><input name='update_customer_state' size='15' value='<?php echo tep_html_quotes($pos_order->customer['state']); ?>'> </td>
      </tr>
      <tr>
        <td class="editOrder"><b><?php echo ENTRY_POST_CODE ?></b></td>
        <td><input name='update_customer_postcode' size='5' value='<?php echo $pos_order->customer['postcode']; ?>'></td>
      </tr>
      <tr>
        <td class="editOrder"><b><?php echo ENTRY_COUNTRY ?></b></td>
        <td><input name='update_customer_country' size='37' value='<?php echo tep_html_quotes($pos_order->customer['country']); ?>'></td>
      </tr>

     </table>
    </td>
  <?php
  }else{

  }
?>

<?php if(POS_SHOW_BILLING == 'true') { ?>
      <td>
       <!-- Billing Address Block -->
       <table border="0" cellspacing="0" cellpadding="2">

      <tr>
        <td colspan='2' class="main">
          <table border="0" cellspacing="0" cellpadding="2" code="infoBox">
          <tr>
            <td class="editOrder"><b><?php echo ENTRY_NAME ?></b></td>
              <td><input name='update_billing_name' size='37' value='<?php echo tep_html_quotes($pos_order->billing['name']); ?>'></td>
            </tr>
            <tr>
              <td class="editOrder"><b><?php echo ENTRY_COMPANY ?></b></td>
              <td><input name='update_billing_company' size='37' value='<?php echo tep_html_quotes($pos_order->billing['company']); ?>'></td>
            </tr>
            <tr>
              <td class="editOrder"><b><?php echo ENTRY_CUSTOMER_ADDRESS ?></b></td>
              <td><input name='update_billing_street_address' size='37' value='<?php echo tep_html_quotes($pos_order->billing['street_address']); ?>'></td>
            </tr>
            <tr>
              <td class="editOrder"><b><?php echo ENTRY_SUBURB ?></b></td>
              <td><input name='update_billing_suburb' size='37' value='<?php echo tep_html_quotes($pos_order->billing['suburb']); ?>'></td>
            </tr>
            <tr>
              <td class="editOrder"><b><?php echo ENTRY_CITY ?></b></td>
              <td><input name='update_billing_city' size='15' value='<?php echo tep_html_quotes($pos_order->billing['city']); ?>'> </td>
            </tr>
            <tr>
              <td class="editOrder"><b><?php echo ENTRY_STATE ?></b></td>
              <td><input name='update_billing_state' size='15' value='<?php echo tep_html_quotes($pos_order->billing['state']); ?>'> </td>
            </tr>
            <tr>
              <td class="editOrder"><b><?php echo ENTRY_POST_CODE ?></b></td>
              <td><input name='update_billing_postcode' size='5' value='<?php echo $pos_order->billing['postcode']; ?>'></td>
            </tr>
            <tr>
              <td class="editOrder"><b><?php echo ENTRY_COUNTRY ?></b></td>
              <td><input name='update_billing_country' size='37' value='<?php echo tep_html_quotes($pos_order->billing['country']); ?>'></td>
            </tr>
          </table>
        </td>
      </tr>
    </table>
      </td>
<?php } ?>


<?php if (POS_SHOW_SHIPPING == 'true') { ?>
      <!-- Shipping Address Block -->
   <td valign="top">
    <table border="0" cellspacing="0" cellpadding="2">
      <tr>
        <td colspan='2' class="main">
          <table border="0" cellspacing="0" cellpadding="2" code="infoBox">
          <tr>
            <td class="editOrder"><b><?php echo ENTRY_NAME ?></b></td>
              <td><input name='update_delivery_name' size='37' value='<?php echo tep_html_quotes($pos_order->delivery['name']); ?>'></td>
            </tr>
            <tr>
              <td class="editOrder"><b><?php echo ENTRY_COMPANY ?></b></td>
              <td><input name='update_delivery_company' size='37' value='<?php echo tep_html_quotes($pos_order->delivery['company']); ?>'></td>
            </tr>
            <tr>
              <td class="editOrder"><b><?php echo ENTRY_CUSTOMER_ADDRESS ?></b></td>
              <td><input name='update_delivery_street_address' size='37' value='<?php echo tep_html_quotes($pos_order->delivery['street_address']); ?>'></td>
            </tr>
            <tr>
              <td class="editOrder"><b><?php echo ENTRY_SUBURB ?></b></td>
              <td><input name='update_delivery_suburb' size='37' value='<?php echo tep_html_quotes($pos_order->delivery['suburb']); ?>'></td>
            </tr>
            <tr>
              <td class="editOrder"><b><?php echo ENTRY_CITY ?></b></td>
              <td><input name='update_delivery_city' size='15' value='<?php echo tep_html_quotes($pos_order->delivery['city']); ?>'> </td>
            </tr>
            <tr>
              <td class="editOrder"><b><?php echo ENTRY_STATE ?></b></td>
              <td><input name='update_delivery_state' size='15' value='<?php echo tep_html_quotes($pos_order->delivery['state']); ?>'> </td>
            </tr>
            <tr>
              <td class="editOrder"><b><?php echo ENTRY_POST_CODE ?></b></td>
              <td><input name='update_delivery_postcode' size='5' value='<?php echo $pos_order->delivery['postcode']; ?>'></td>
            </tr>
            <tr>
              <td class="editOrder"><b><?php echo ENTRY_COUNTRY ?></b></td>
              <td><input name='update_delivery_country' size='37' value='<?php echo tep_html_quotes($pos_order->delivery['country']); ?>'></td>
            </tr>
          </table>
        </td>

     <?php
     }
     ?>
       </tr>
      </table>
      </td>
    </tr>
  </table></td>
      </tr>
<!-- End Addresses Block -->

      <tr>
  <td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
      </tr>

<!-- Begin Phone/Email Block -->
<?php if( (POS_SHOW_CUSTOMER_INFO == 'true')||(POS_SHOW_BILLING == 'true')||(POS_SHOW_SHIPPING == 'true') ) { ?>
      <tr>
        <td><table border="0" cellspacing="0" cellpadding="2" code="infoBox">
          <tr>
            <td class="main"><b><?php echo ENTRY_TELEPHONE_NUMBER; ?></b></td>
            <td class="main"><input name='update_customer_telephone' size='15' value='<?php echo $pos_order->customer['telephone']; ?>'></td>
          </tr>
          <tr>
            <td class="main"><b><?php echo ENTRY_EMAIL_ADDRESS; ?></b></td>
            <td class="main"><input name='update_customer_email_address' size='35' value='<?php echo $pos_order->customer['email_address']; ?>'></td>
          </tr>
        </table></td>
      </tr>
     <?php
     }
     ?>
<!-- End Phone/Email Block -->

      <tr>
  <td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
      </tr>
<!-- Begin Products Listing Block -->
      <tr>
  <td><table border="0" width="100%" cellspacing="0" cellpadding="2">
    <tr class="dataTableHeadingRow">
      <td class="dataTableHeadingContent" colspan="2"><?php echo TABLE_HEADING_PRODUCTS; ?></td>
      <td class="dataTableHeadingContent"><?php echo TABLE_HEADING_PRODUCTS_MODEL; ?></td>
      <td class="dataTableHeadingContent" align="center"><?php echo TABLE_HEADING_TAX; ?></td>
      <td class="dataTableHeadingContent" align="right"><?php echo TABLE_HEADING_BASE_PRICE; ?></td>
      <td class="dataTableHeadingContent" align="right"><?php echo TABLE_HEADING_UNIT_PRICE; ?></td>
      <td class="dataTableHeadingContent" align="right"><?php echo TABLE_HEADING_TOTAL_PRICE; ?></td>
<?php
  if ( DISPLAY_PRICE_WITH_TAX == 'true'){
    echo  '<td class="dataTableHeadingContent" align="right">' . TABLE_HEADING_TOTAL_PRICE_TAXED . '</td>';
    }
;?>
  </tr>

  <!-- Begin Products Listings Block -->
  <?php
        // Override order.php code's Field Limitations
    $index = 0;
    $pos_order->products = array();
    $poss_products_query = tep_db_query("select * from " . TABLE_POS_PRODUCTS . " where pos_id = '" . (int)$posID . "'");
    while ($poss_products = tep_db_fetch_array($poss_products_query)) {
    $pos_order->products[$index] = array('qty' => $poss_products['products_quantity'],
                                        'name' => str_replace("'", "&#39;", $poss_products['products_name']),
                                        'model' => $poss_products['products_model'],
                                        'tax' => $poss_products['products_tax'],
                                        'price' => $poss_products['products_price'],
                                        'final_price' => $poss_products['final_price'],
                                        'pos_products_id' => $poss_products['pos_products_id'],
                                        'products_id' => $poss_products['products_id']);

    $subindex = 0;

    $attributes_query_string = "select * from " . TABLE_POS_PRODUCTS_ATTRIBUTES . " where pos_id = '" . $posID . "' and pos_products_id = '" . $poss_products['pos_products_id'] . "'";
    $attributes_query = tep_db_query($attributes_query_string);
    if (tep_db_num_rows($attributes_query)) {

    while ($attributes = tep_db_fetch_array($attributes_query)) {

      $pos_order->products[$index]['attributes'][$subindex] = array('option' => $attributes['products_options'],
                                                               'value' => $attributes['products_options_values'],
                                                               'prefix' => $attributes['price_prefix'],
                                                               'price' => $attributes['options_values_price'],
                                                               'orders_products_attributes_id' => $attributes['orders_products_attributes_id']);
    $subindex++;
    }
    }
    $index++;
    }
  for ($i=0; $i<sizeof($pos_order->products); $i++) {
    $pos_products_id = $pos_order->products[$i]['products_id'];
    $RowStyle = "dataTableContent";
if ($pos_order->products[$i]['products_id'] != 0) {

    echo '    <tr class="dataTableRow">' . "\n" ;
 if (POS_EDIT_PRICE == 'true'){
   echo   '      <td class="' . $RowStyle . '" valign="top" align="left">' . "<input name='update_products[$pos_products_id][qty]' type='text' size='2' value='" . $pos_order->products[$i]['qty'] . "'>&nbsp;x</td>\n" .
          '      <td class="' . $RowStyle . '" valign="top">' . "<input name='update_products[$pos_products_id][name]' type='text' size='25' value='" . $pos_order->products[$i]['name'] . "'>";
  }else{
   echo   '      <td class="' . $RowStyle . '" valign="top" align="left">' . "<input name='update_products[$pos_products_id][qty]' size='2' value='" . $pos_order->products[$i]['qty'] . "'>&nbsp;x</td>\n" .
          '      <td class="' . $RowStyle . '" valign="top">' . $pos_order->products[$i]['pos_products_id'] . $pos_order->products[$i]['name'] . "<input name='update_products[$pos_products_id][name]' type='hidden' size='25' value='" . $pos_order->products[$i]['name'] . "'>";
  }
    // Has Attributes?
   if (isset($pos_order->products[$i]['attributes'])){
    if (sizeof($pos_order->products[$i]['attributes']) > 0) {
      for ($j=0; $j<sizeof($pos_order->products[$i]['attributes']); $j++) {
        $poss_products_attributes_id = $pos_order->products[$i]['attributes'][$j]['orders_products_attributes_id'];
        echo '<br><small>&nbsp;<i> - ' . $pos_order->products[$i]['attributes'][$j]['option'] . ' : ' . $pos_order->products[$i]['attributes'][$j]['value'] . ' ' . $pos_order->products[$i]['attributes'][$j]['prefix'] . ' ' . $currencies->format($pos_order->products[$i]['attributes'][$j]['price']) ;
        echo '</i></small>';
      }
    }
  }
if (POS_EDIT_PRICE == 'true'){
  $edit_price =   ('      <td class="' . $RowStyle . '" align="right" valign="top">' . "<input name='update_products[$pos_products_id][final_price]' size='5' value='" . number_format($pos_order->products[$i]['final_price'], 2, '.', '') . "'>" . '</td>');


} else {
  $edit_price =   ('      <td class="' . $RowStyle . '" align="right" valign="top">' . $currencies->format($pos_order->products[$i]['final_price'], 1, $pos_order->info['currency'], $pos_order->info['currency_value']) . "<input name='update_products[$pos_products_id][final_price]' type='hidden' size='5' value='" . number_format($pos_order->products[$i]['final_price'], 2, '.', '') . "'>" . '</td>' );
}

    echo '      </td>' . "\n" .
         '      <td class="' . $RowStyle . '" valign="top">' . $pos_order->products[$i]['model'] . "<input name='update_products[$pos_products_id][model]' type='hidden' size='12' value='" . $pos_order->products[$i]['model'] . "'>" . '</td>' . "\n" .
         '      <td class="' . $RowStyle . '" align="center" valign="top">' . "<input name='update_products[$pos_products_id][tax]' size='3' value='" . tep_display_tax_value($pos_order->products[$i]['tax']) . "'>" . '</td>' . "\n" .
         '      <td class="' . $RowStyle . '" align="right" valign="top">' . $currencies->format($pos_order->products[$i]['price'], 1, $pos_order->info['currency'], $pos_order->info['currency_value']). "<input name='update_products[$pos_products_id][price]' type='hidden' size='5' value='" . number_format($pos_order->products[$i]['price'], 2, '.', '') . "'>" . '</td>' . "\n" .
         $edit_price . "\n" .
         '      <td class="' . $RowStyle . '" align="right" valign="top">' . $currencies->format($pos_order->products[$i]['final_price'] * $pos_order->products[$i]['qty'], 1, $pos_order->info['currency'], $pos_order->info['currency_value']) . '</td>' . "\n" ;
     if ( DISPLAY_PRICE_WITH_TAX == 'true'){
    echo '      <td class="dataTableContent" align="right" valign="top"><b>' . $currencies->format(tep_add_tax($pos_order->products[$i]['final_price'], $pos_order->products[$i]['tax']) * $pos_order->products[$i]['qty'], 1, $pos_order->info['currency'], $pos_order->info['currency_value']) . '</b></td>' . "\n";
      }
    echo '    </tr>' . "\n";
  }
  }


  ?>
   <tr>
          <td class="main" valign='top'><?php echo '<a href="' . tep_href_link(FILENAME_CREATE_POS_ORDERS, 'posID=' . $posID . '&action=add_product&step=1') . '">'. tep_image_button('button_add_product.gif', 'Add a product') . '&nbsp;</a></td>' ;?>
          <td class="main" valign='top'><?php echo tep_image_submit('button_update.gif', 'Update Products') . '&nbsp;</a></td>' ;?>
   <tr>
        </table></td>
    </tr>
  <!-- End Products Listings Block -->
   <tr>
        <td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
   </tr>

<!-- Begin Payment Block -->
<?php
  if (isset($_GET['payment_error']) && is_object(${$_GET['payment_error']}) && ($error = ${$_GET['payment_error']}->get_error())) {
?>
      <tr>
        <td><table border="0" width="100%" cellspacing="0" cellpadding="2">
          <tr>
            <td class="main"><b><?php echo tep_output_string_protected($error['title']); ?></b></td>
          </tr>
        </table></td>
      </tr>
      <tr>
        <td>
        <table border="0" width="100%" cellspacing="1" cellpadding="2" code="infoBoxNotice">
          <tr class="infoBoxNoticeContents">
            <td>
            <table border="0" width="100%" cellspacing="0" cellpadding="2">
              <tr>
                <td><?php echo tep_draw_separator('pixel_trans.gif', '10', '1'); ?></td>
                <td class="main" width="100%" valign="top"><?php echo tep_output_string_protected($error['error']); ?></td>
                <td><?php echo tep_draw_separator('pixel_trans.gif', '10', '1'); ?></td>
              </tr>
            </table></td>
          </tr>
        </table>
       </td>
          </tr>
        </table>
       <tr>
<?php
  }
?>

 <!-- end payment error -->
 <!-- begin payment -->
    <tr>
      <td align="right"><table border="0" cellspacing="0" cellpadding="2" width="100%">
        <tr>
        <td align='left'>
<?php	require('includes/pos_order/payment_box.php');?>

        </td>
        </tr>
       </table></td>
    </tr> <!-- end payment -->
 <!-- credit selection -->
<?php
// echo $pos_total_modules->credit_selection() ;//rmh M-S_ccgv
?>


 <!-- credit selection -->
    <tr>
      <td colspan="3"><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
    </tr>

     <!-- begin Shipping Block -->
    <tr>
      <td align="right">
        <table border="0" cellspacing="0" cellpadding="2" width="100%">
        <tr>
        <td align='left'>
<?php	require('includes/pos_order/ajax_shipping.php');?>

        </td>
        <td  code="infoBox"><div id="contentLYR" align="right"  style="margin-right: 10px;">
</div></td></tr>
        </tr>

       </table></td>
    </tr>

       <!-- End SHipping Block -->

    <tr>
      <td colspan="3"><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
    </tr>

  <!-- Begin Order Total Block -->
<?php
 $show_total = tep_db_prepare_input($_GET['show_total']);

 // if ($show_total ==1)  {
//  require('includes/pos_order/total_box.php');
//  }
  ?>
  <!-- Begin Order Total Block -->
    <tr>
      <td align="right" colspan="8">
        <table border="0" cellspacing="0" cellpadding="2" width="100%">
        <tr>
        <td align='center' valign='top'></td>
        <td align='right'>
        <table border="0" cellspacing="0" cellpadding="2">
<?php

        // Override order.php Class's Field Limitations
    $totals_query = tep_db_query("select * from " . TABLE_POS_TOTAL . " where pos_id = '" . (int)$posID . "' order by sort_order");
    $pos_order->totals = array();
    while ($totals = tep_db_fetch_array($totals_query)) { $pos_order->totals[] = array('title' => $totals['title'], 'text' => $totals['text'], 'code' => $totals['code'], 'value' => $totals['value'], 'pos_total_id' => $totals['pos_total_id']); }

  $TotalsArray = array();
  for ($i=0; $i<sizeof($pos_order->totals); $i++) {
    $TotalsArray[] = array("Name" => $pos_order->totals[$i]['title'], "Price" => number_format($pos_order->totals[$i]['value'], 2, '.', ''), "Code" => $pos_order->totals[$i]['code'], "TotalID" => $pos_order->totals[$i]['pos_total_id']);
    $TotalsArray[] = array("Name" => "          ", "Price" => "", "Code" => "ot_custom", "TotalID" => "0");
  }

  array_pop($TotalsArray);
  foreach($TotalsArray as $TotalIndex => $TotalDetails)
  {
    $TotalStyle = "smallText";
    if(($TotalDetails["Code"] == "ot_subtotal") || ($TotalDetails["Code"] == "ot_total"))
    {
      echo  '       <tr>' . "\n" .
        '   <td class="main" align="right"><b>' . $TotalDetails["Name"] . '</b></td>' .
        '   <td class="main" align="right"><b>' . $TotalDetails["Price"] .
            "<input name='update_totals[$TotalIndex][title]' type='hidden' value='" . trim($TotalDetails["Name"]) . "' >" .
            "<input name='update_totals[$TotalIndex][value]' type='hidden' value='" . $TotalDetails["Price"] . "' size='6' >" .
            "<input name='update_totals[$TotalIndex][code]' type='hidden' value='" . $TotalDetails["Code"] . "'>\n" .
            "<input type='hidden' name='update_totals[$TotalIndex][total_id]' value='" . $TotalDetails["TotalID"] . "'>" . '</b></td>' .
        '       </tr>' . "\n";
    }
    elseif($TotalDetails["Code"] == "ot_customer_discount")
        {
          echo  '       <tr>' . "\n" .
            '   <td class="main" align="right"><font color="#FF0000">' . ENTRY_CUSTOMER_DISCOUNT . '<b>' . $TotalDetails["Name"] . '</b></font></td>' .
            '   <td align="right" SPAN class="' . $TotalStyle . '">' . "<input name='update_totals[$TotalIndex][value]' size='6' value=' " . $TotalDetails["Price"] . "'></SPAN>" .
                "<input name='update_totals[$TotalIndex][title]' type='hidden' value='" . trim($TotalDetails["Name"]) . "' >" .
                "<input name='update_totals[$TotalIndex][code]' type='hidden' value='" . $TotalDetails["Code"] . "'>\n" .
                "<input type='hidden' name='update_totals[$TotalIndex][total_id]' value='" . $TotalDetails["TotalID"] . "'>" . '</b></td>' .
            '       </tr>' . "\n";
    }
//discounts
    elseif(($TotalDetails["Code"] == "ot_gv") || ($TotalDetails["Code"] == "ot_coupon"))
        {
          echo  '       <tr>' . "\n" .
            '   <td class="main" align="right">' . ENTRY_CUSTOMER_GV . '<b>' . $TotalDetails["Name"] . '</b></td>' .
            '   <td align="right" class="' . $TotalStyle . '">' . "<input name='update_totals[$TotalIndex][value]' size='6' value=' " . $TotalDetails["Price"] . "'>" .
                "<input name='update_totals[$TotalIndex][title]' type='hidden' value='" . trim($TotalDetails["Name"]) . "' >" .
                "<input name='update_totals[$TotalIndex][code]' type='hidden' value='" . $TotalDetails["Code"] . "'>\n" .
                "<input type='hidden' name='update_totals[$TotalIndex][total_id]' value='" . $TotalDetails["TotalID"] . "'>" . '</b></td>' .
            '       </tr>' . "\n";
    }
    elseif($TotalDetails["Code"] == "ot_tax")
 //taxes
    {
      echo  '       <tr>' . "\n" .
        '   <td class="main" align="right"><b>' . $TotalDetails["Name"] . '</b></td>' .
        '   <td class="main" align="right"><b>' . $TotalDetails["Price"] .
            "<input name='update_totals[$TotalIndex][title]' type='hidden' value='" . trim($TotalDetails["Name"]) . "' >" .
            "<input name='update_totals[$TotalIndex][value]' type='hidden' value='" . $TotalDetails["Price"] . "' size='6' >" .
            "<input name='update_totals[$TotalIndex][code]' type='hidden' value='" . $TotalDetails["Code"] . "'>\n" .
            "<input type='hidden' name='update_totals[$TotalIndex][total_id]' value='" . $TotalDetails["TotalID"] . "'>" . '</b></td>' .
        '       </tr>' . "\n";
    }
        //  Shipping
    elseif($TotalDetails["Code"] == "ot_shipping")
    {
      //list exsisting shipping if not in current order_ship_meahtods table
           $orders_ship_methodA[] = array('id'   => $TotalDetails["Name"],
                                                    'text' => $TotalDetails["Name"]);

                  //  $orders_ship_method1 = array_merge($orders_ship_method, $orders_ship_methodA) ;
                  // check to see if meathod in totals  is in same as in order_ship_meahtods table
                          // if 0 merge if 1 dont merge

                     //   $orders_ship_method1 = array_merge($orders_ship_methodA, $orders_ship_method) ;


      echo  ' <tr>' . "\n" .
            ' <td class="main" align="right"><b>' . $TotalDetails["Name"]. '</b></td>' . "\n";
      echo  ' <td align="right" class="' . $TotalStyle . '">' . "<input name='update_totals[$TotalIndex][value]' size='6' value='" . $TotalDetails["Price"] . "'>" .
            "<input type='hidden' name='update_totals[$TotalIndex][code]' value='" . $TotalDetails["Code"] . "'>" .
            "<input type='hidden' name='update_totals[$TotalIndex][total_id]' value='" . $TotalDetails["TotalID"] . "'>" . '</td>' .
        '       </tr>' . "\n";
    }
    // End Shipping
    else
    {
      echo  '       <tr>' . "\n" .
          '   <td class="main" align="right"><b>' . $TotalDetails["Name"] . '</b></td>' .
        '   <td align="right" class="' . $TotalStyle . '">' . "<input type='hidden' name='update_totals[$TotalIndex][value]' size='6' value='" . $TotalDetails["Price"] . "'>" .
            "<input type='hidden' name='update_totals[$TotalIndex][title]' value='" . trim($TotalDetails["Name"]) . "' >" .
            "<input type='hidden' name='update_totals[$TotalIndex][code]' value='" . $TotalDetails["Code"] . "'>" .
            "<input type='hidden' name='update_totals[$TotalIndex][total_id]' value='" . $TotalDetails["TotalID"] . "'>" .
            '</td>' . "\n" .
        '       </tr>' . "\n";
    }
  }
       if ($payment == 'cis'){
      echo   '   <td class="main" align="right"><b>' . 'Amount tendered' . '</b></td>' .
  	         '   <td align="right" class="' . $TotalStyle . '">' . "<input name='update_totals[$TotalIndex][value]' size='6' value=''>" .
              "<input type='hidden' name='update_totals[$TotalIndex][title]' value='" . 'Amount tendered' . "' >" .
              "<input type='hidden' name='update_totals[$TotalIndex][code]' value='" . ot_amount_tendered . "'>" .
             '</td>' . "\n" .
			 '       </tr>' . "\n";

  }
?>
        </table>
        </td>
        </tr>
        </table>
      </td>
    </tr>
  <!-- End Order Total Block -->


      <tr>
        <td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
      </tr>


      <tr>
  <td colspan="3" align="right">
  <?php echo '<a href="' . tep_href_link(FILENAME_POS_ORDERS_PROCESS, 'posID=' . $posID . '&action=process') . '">' . tep_image_button('button_removeccinfo.gif', 'Confirm Order') .
  '&nbsp;</a>' . tep_image_submit('button_update.gif', 'Pre-confirm CC Check') . '&nbsp;<a href="' . tep_href_link(FILENAME_CREATE_POS_ORDERS, tep_get_all_get_params(array('action')),  'SSL') . '">' .
  tep_image_button('button_back.gif', IMAGE_BACK) . '</a>'; ?>


  </td>
  </tr>
  <tr>
  <td colspan="3" align="right"><?php
    if (isset($_GET[tep_session_name()])) {
      $oscid = '&' . tep_session_name() . '=' . $_GET[tep_session_name()];
    } else {
      $oscid = '';
    }
   // echo ' </a> <a href="javascript:popupWindow(\'' .  (HTTP_SERVER . DIR_WS_ADMIN . FILENAME_ORDERS_INVOICE) . '?' . (tep_get_all_get_params(array('posID')) . 'posID=' . $posID) . $oscid . '\')">' . tep_image_button('button_invoice.gif', IMAGE_ORDERS_INVOICE) . '</a><a href="javascript:popupWindow(\'' .  (HTTP_SERVER . DIR_WS_ADMIN . FILENAME_ORDERS_PACKINGSLIP) . '?' . (tep_get_all_get_params(array('posID')) . 'posID=' . $posID) . $oscid . '\')">' . tep_image_button('button_packingslip.gif', IMAGE_ORDERS_PACKINGSLIP) . '</a>'; ?></td>



    </tr>
      </form>
   <!-- status block -->
    <tr>
      <td align="right" colspan="8">
        <table border="0" cellspacing="0" cellpadding="2" width="100%">

                     <tr>
                      <td>
                      <?php echo tep_draw_form('status', FILENAME_CREATE_POS_ORDERS, tep_get_all_get_params(array('action')) . 'action=update_comments'); ?>
                        <table width="100%" border="0" cellpadding="0" cellspacing="0" class="main">
                        <tr>
                           <td class="main"><br><b><?php echo TABLE_HEADING_COMMENTS; ?></b></td>
                       </tr>
                       <tr>
                           <td><?php echo tep_draw_separator('pixel_trans.gif', '1', '5'); ?></td>
                       </tr>
                          <tr>
                         <td class="main">
      <?php
                echo tep_draw_textarea_field('comments', 'soft', '60', '5');
        ?>
                         </td>
                       </tr>
                           <td><?php echo tep_image_submit('button_update.gif', IMAGE_UPDATE); ?></td>
                       </tr>
                        </table></form>
                    </td>

                     <td>
                        <?php echo tep_draw_form('status', FILENAME_CREATE_POS_ORDERS, tep_get_all_get_params(array('action')) . 'action=update_admin_comments'); ?>
                        <table width="100%" border="0" cellpadding="0" cellspacing="0" class="main">
                          <tr>
                            <td class="main"><b><?php echo TABLE_HEADING_ADMIN_COMMENTS; ?></b></td>
                          </tr>
                          <tr>
                            <td><?php echo tep_draw_textarea_field('admin_comments', 'soft', '60', '5'); ?></td>
                          </tr>
                          <tr>
                            <td><?php echo tep_image_submit('button_update.gif', IMAGE_UPDATE); ?></td>
                          </tr>
                        </table>

                        </td>

                    </tr>
               </td>
            </table></form>
     </tr>
 <!-- status block -->
<?php
  }
//************************************************** add product ****************************
if($action == "add_product")
{

    if (isset($_GET['add_product_categories_id'])) {
      $add_product_categories_id = $_GET['add_product_categories_id'] ;
    }else if (isset($_POST['add_product_categories_id'])){
      $add_product_categories_id = $_POST['add_product_categories_id'] ;
    } else {
      $add_product_categories_id  = '' ;
    }

    if (isset($_GET['add_product_products_id'])) {
      $add_product_products_id = $_GET['add_product_products_id'] ;
    }else if (isset($_POST['add_product_products_id'])){
      $add_product_products_id = $_POST['add_product_products_id'] ;
    } else {
      $add_product_products_id  = '' ;
    }
  $customer_id = $pos_order->customer['id'];
   //gets customer group ID


  // ############################################################################
  //   Get List of All Products
  // ############################################################################
  $parent_ids = array();
  $parent_query = tep_db_query("SELECT DISTINCT products_parent_id
                                FROM " . TABLE_PRODUCTS . "
                                WHERE products_parent_id <> 0");
  while($parent = tep_db_fetch_array($parent_query)) {
    $parent_ids[$parent['products_parent_id']] = 1;
  }
  ksort($parent_ids);

    $result = tep_db_query("SELECT
     pd.products_name,
     p.products_id,
     cd.categories_name,
     ptc.categories_id
    FROM " . TABLE_PRODUCTS . " p,
         " . TABLE_PRODUCTS_DESCRIPTION . " pd,
         " . TABLE_PRODUCTS_TO_CATEGORIES . " ptc,
         " . TABLE_CATEGORIES_DESCRIPTION . " cd
    where cd.categories_id=ptc.categories_id
     and ptc.products_id=p.products_id
     and (p.products_status = '1'
     or (p.products_status <> '1' and p.products_parent_id <> 0))
     and p.products_id=pd.products_id
     and pd.language_id = '" . (int)$languages_id . "'
                            and cd.language_id = '" . (int)$languages_id . "'
    ORDER BY cd.categories_name");
    while($row = tep_db_fetch_array($result))
    {
//      if (tep_db_num_rows($result_new) > 0) {
      if (isset($parent_ids[$row['products_id']])) {
        continue;
      }
      extract($row,EXTR_PREFIX_ALL,"db");
      $ProductList[$db_categories_id][$db_products_id] = $db_products_name;
      $CategoryList[$db_categories_id] = $db_categories_name;
      $LastCategory = $db_categories_name;
    }
    $LastOptionTag = "";
    $ProductSelectOptions = "<option value='0'>".DONT_ADD_NEW_PRODUCT . $LastOptionTag . "\n";
    $ProductSelectOptions .= "<option value='0'>&nbsp;" . $LastOptionTag . "\n";
    foreach($ProductList as $Category => $Products)
    {
      $ProductSelectOptions .= "<option value='0'>$Category" . $LastOptionTag . "\n";
      $ProductSelectOptions .= "<option value='0'>---------------------------" . $LastOptionTag . "\n";
      asort($Products);
      foreach($Products as $Product_ID => $Product_Name)
      {
        $ProductSelectOptions .= "<option value='$Product_ID'> &nbsp; $Product_Name" . $LastOptionTag . "\n";
      }

      if($Category != $LastCategory)
      {
        $ProductSelectOptions .= "<option value='0'>&nbsp;" . $LastOptionTag . "\n";
        $ProductSelectOptions .= "<option value='0'>&nbsp;" . $LastOptionTag . "\n";
      }
    }


  // ############################################################################
  //   Add Products Steps
  // ############################################################################

	require('includes/pos_order/show_products.php');

}// end add product
?>
    </table></td>
<!-- body_text_eof //-->
  </tr>
</table>
<!-- body_eof //-->

<!-- footer //-->
<?php require(DIR_WS_INCLUDES . 'footer.php'); ?>
<!-- footer_eof //-->
<br>
</body>
</html>
<?php
require(DIR_WS_INCLUDES . 'application_bottom.php');
?>
